﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace IMTApp
{
    public class QueryEndorsementMSG : XMLMessage
    {
        private XmlDocument _xml;
        private XmlDeclaration _xmlDec;
        private readonly MessageData _msg;
        GPM.QuickQuote.Logging.GpmLogger logger = new GPM.QuickQuote.Logging.GpmLogger();
        
        // 
        // Author: Elliott Perry 21/03/2011
        // 
        // This is the child class for QueryEndorsement messages for Lime Street. It inherits from the base XMLMessage class.
        //

        //testc Constructor
        public QueryEndorsementMSG()
        {

        }


        public QueryEndorsementMSG(string docPath)
        {
            logger.LogInfo("DEBUG: Query Endorsement MSG Object Created: Path = " + docPath + " at " + DateTime.Now.ToString("yyyy MM dd hh mm ss"));

            GlobalData.rootPath = @"C:\TSMSG\";

            //Create a MessageData Object;
            //_msg = new MessageData();

            disectMessage(docPath);

        }

        private void disectMessage(string docPath)
        {

            
              //get delimiter from Global Data
            string delimiter = GlobalData.MessageDelimiter;
            List<string> data = new List<string>();
          string strXML  = string.Empty;

            //Open up FileStream to Doc
            try
            {
                using (StreamReader reader = new StreamReader(docPath))
                {
                    //This string object
                    StringBuilder inputString = new System.Text.StringBuilder();
                   
                    //Read past the QueryEndorsement,
                    for (int i = 0; i < 17; i++)
                        reader.Read();
                    while (reader.Peek() != GlobalData.MessageDelimiterASCII)
                    {
                        inputString.Append(char.ConvertFromUtf32(reader.Read()));
                    }
                       strXML = inputString.ToString();
                }

                buildXMLString(strXML);
            }
            catch (Exception e)
            {
                logger.LogError("ERROR: " + e);
            }
            
        }

        public void buildXMLString(string strXML)
        {
            _xml = new XmlDocument();


            //_xmlDec = _xml.CreateXmlDeclaration("1.0", null, null);

            //XmlElement root = _xml.DocumentElement;
            //_xml.InsertBefore(_xmlDec, root);

            XmlElement PackageEL = (XmlElement)_xml.AppendChild(_xml.CreateElement("Package"));


            XmlElement SecurityEL = (XmlElement)PackageEL.AppendChild(_xml.CreateElement("Security"));

            XmlElement SenderIdentEL = (XmlElement)SecurityEL.AppendChild(_xml.CreateElement("SenderIdentification"));
            SenderIdentEL.InnerText = "GPM";
            XmlElement AuthTokenEL = (XmlElement)SecurityEL.AppendChild(_xml.CreateElement("AuthenticationToken"));
            //AuthTokenEL.InnerText = Msg.AuthenticationToken;
            AuthTokenEL.InnerText = generateSecurityToken();

            XmlElement WorkflowEL = (XmlElement)PackageEL.AppendChild(_xml.CreateElement("Workflow"));
            
            XmlElement ReferenceEL = (XmlElement)WorkflowEL.AppendChild(_xml.CreateElement("Reference"));
            ReferenceEL.InnerText = strXML;
           
            XmlElement OperationEL = (XmlElement)WorkflowEL.AppendChild(_xml.CreateElement("Operation"));
            OperationEL.InnerText = "QueryEndorsementStatus";

            XmlElement TechnicalStatusEL = (XmlElement)WorkflowEL.AppendChild(_xml.CreateElement("TechnicalStatus"));
            TechnicalStatusEL.InnerText = "";

            XmlElement DetailsEL = (XmlElement)WorkflowEL.AppendChild(_xml.CreateElement("Details"));
            DetailsEL.InnerText = "Detailed";

            logger.LogInfo("DEBUG: " + _xml.OuterXml.ToString());

            SaveXml(_xml.OuterXml.ToString());
        }

        

    }
}

